﻿using System.Collections.Generic;
using System.Linq;

namespace CRMS.Models
{
    public class ClassRoomRep
    {
        private StoreDBDataContext storeDB = new StoreDBDataContext();
        //
        //Query Methods
        /// <summary>
        /// 获取所有教室信息
        /// </summary>
        /// <returns>所有教室信息</returns>
        public IQueryable<ClassroomInfo> getAllClassroomInfos()
        {
            return from c in storeDB.Classrooms
                   select new ClassroomInfo(c);
        }
        /// <summary>
        /// 通过教学楼ID、星期、节次获取空教室
        /// </summary>
        /// <param name="bID">教学楼ID</param>
        /// <param name="w">星期</param>
        /// <param name="p">节次</param>
        /// <returns></returns>
        public IQueryable<FreeClassRoom> getFreeClassRoom(string bID, string w, string p)
        {
            var obj = from tuc in storeDB.ToUseClassrooms
                      where tuc.Weekday == w && tuc.Period == p
                      select tuc.ClassroomId;
            //return from c in storeDB.Classrooms
            //       where !obj.Contains(c.ClassroomId)
            //       select new FreeClassRoom(w, p, c.ClassroomId, c.TeachingBuilding.TeachingBuildingName);
            var result = from c in storeDB.Classrooms
                         where !obj.Contains(c.ClassroomId) && c.TeachingBuildingId == bID
                         select new FreeClassRoom(w, p, c.ClassroomId, c.TeachingBuilding.TeachingBuildingName);
            System.Diagnostics.Debug.WriteLine(result);
            return result;
        }
        /// <summary>
        /// 通过教室号获取教师的使用情况
        /// </summary>
        /// <param name="cid">教室编号</param>
        /// <returns>教师使用教室任意时段信息</returns>
        public IQueryable<TeacherUseClassroomInfo> getClassroomStateByCID(int cid)
        {
            return from r in
                       (from cr in storeDB.Classrooms.Where(c => c.ClassroomId == cid) select cr)
                   join t in
                       (from tc in storeDB.TeacherCourses.Where(tc => tc.ClassroomId == cid) select tc)
                   on r.ClassroomId equals t.ClassroomId
                   select new TeacherUseClassroomInfo(r, t);
        }
        /// <summary>
        /// 通过教学楼名称和教室号获取教师的使用情况
        /// </summary>
        /// <param name="cid">教室编号</param>
        /// <param name="tbName">教学楼名称</param>
        /// <returns>教师使用教室任意时段信息</returns>
        public IQueryable<TeacherUseClassroomInfo> getCRMByCIDAndBName(int cid, string tbName)
        {
            return from r in
                       (from cr in storeDB.Classrooms.Where(c => c.ClassroomId == cid &&
            c.TeachingBuilding.TeachingBuildingName == tbName)
                        select cr)
                   join t in
                       (from tc in storeDB.TeacherCourses.Where(tc => tc.ClassroomId == cid) select tc)
                   on r.ClassroomId equals t.ClassroomId
                   select new TeacherUseClassroomInfo(r, t);
        }
        /// <summary>
        /// 获取所有的教室使用信息
        /// </summary>
        /// <returns>返回所有使用的教室</returns>
        public IQueryable<ToUseClassroom> getToUseClassroom()
        {
            return from tuc in storeDB.ToUseClassrooms
                   select tuc;
        }
        /// <summary>
        /// 获取特定的教室使用信息
        /// </summary>
        /// <param name="bid">教学楼ID</param>
        /// <param name="cid">教室ID</param>
        /// <param name="uid">用户ID</param>
        /// <param name="w">星期</param>
        /// <param name="p">节次</param>
        /// <returns>返回特定的教室使用信息</returns>
        public ToUseClassroom getOneInstance(string bid, int cid, string uid, string w, string p)
        {
            return storeDB.ToUseClassrooms.SingleOrDefault(t => t.TeachingBuildingId == bid
                     && t.ClassroomId == cid && t.UserId == uid && t.Weekday == w && t.Period == p);
        }

        /// <summary>
        /// 获取教学楼信息
        /// </summary>
        /// <returns>全部教学楼信息</returns>
        public IQueryable<TeachingBuilding> getTeachingBuilding()
        {
            return from tb in storeDB.TeachingBuildings
                   select tb;
        }
        /// <summary>
        /// 通过教室编号获取教室信息
        /// </summary>
        /// <param name="cid">教室编号</param>
        /// <returns>教室信息</returns>
        public IQueryable<ToUseClassroom> getCRMByBCID(string bName, int cID)
        {
            return from c in storeDB.ToUseClassrooms
                   where c.ClassroomId == cID && c.TeachingBuilding.TeachingBuildingName == bName
                   select c;
        }
        /// <summary>
        /// 通过教学楼编号、教室编号获取特定教室
        /// </summary>
        /// <param name="bid">教学楼编号</param>
        /// <param name="cid">教室编号</param>
        /// <returns>返回特定教室</returns>
        public Classroom getClassroomByBIDAndCID(string bid, int cid)
        {
            return storeDB.Classrooms.SingleOrDefault(c => c.TeachingBuildingId == bid && c.ClassroomId == cid);
        }
        /// <summary>
        /// 通过教学楼名称、教室编号获取特定教室
        /// </summary>
        /// <param name="bid">教学楼名称</param>
        /// <param name="cid">教室编号</param>
        /// <returns>返回特定教室</returns>
        public Classroom getClassroomByBNameAndCID(string bName, int cid)
        {
            return storeDB.Classrooms.SingleOrDefault(c => c.TeachingBuilding.TeachingBuildingName == bName && c.ClassroomId == cid);
        }

        //
        //Insert/Delete
        /// <summary>
        /// 向数据库教室表添加一条教室记录
        /// </summary>
        /// <param name="classRm">教室对象参数</param>
        public void Add(Classroom classRm)
        {
            storeDB.Classrooms.InsertOnSubmit(classRm);
        }
        /// <summary>
        /// 从数据库教室表中删除一条教室记录,必须先将其所关联的表删除
        /// </summary>
        /// <param name="classRm">教师对象参数</param>
        public void Delete(Classroom classRm)
        {
            storeDB.Classrooms.DeleteOnSubmit(classRm);
        }

        //
        //Persistence
        /// <summary>
        /// 把更改保存到数据库
        /// </summary>
        public void Save()
        {
            storeDB.SubmitChanges();
        }

        
    }
}
